package com.winter.collection.List;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: Collection
 * @description: 集合的fail-fast机制
 * @author: Mr.Liang
 * @create: 2018-01-08 09:50
 **/
public class failFast {
    /*ArrayList的定义如下*/
    private static List<String> list = new ArrayList<String>();
    /*CopyOnWriterArrayList的定义如下*/
//    private static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();

    public static void main(String[] args) {
        /*同时启动两个线程对List进行操作*/
        new ThendOne().start();
        new ThendTwo().start();
    }

    /*向List中依次添加0，1,2,3,4,5，每添加一个就遍历一次List*/
    private static class ThendOne extends Thread {
        public void run() {
            int i = 0;
            while (i < 6) {
                list.add(String.valueOf(i));
                printAll();
                i++;
            }
        }
    }

   /*向List中依次添加10,11,12,13,14,15，每添加一个就遍历一次List*/
    private static class ThendTwo extends Thread {
        public void run() {
            int i = 10;
            while (i < 16) {
                list.add(String.valueOf(i));
                printAll();
                i++;
            }
        }
    }

    private static void printAll() {
        System.out.println("");
        String value = null;
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            value = (String) iterator.next();
            System.out.println(value + ",");
        }

    }

}
